package com.quicksignin.service.impl;

import com.ndktools.javamd5.Mademd5;
import com.quicksignin.common.ServerResponse;
import com.quicksignin.dao.UserMapper;
import com.quicksignin.pojo.User;
import com.quicksignin.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created by GHD on 2018/10/10.
 */
@Service
public class UserServiceImpl implements IUserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public ServerResponse<User> login(String teacherAccount, String teacherPassword) {
//        System.out.println("service" + teacherAccount + teacherPassword);
        int resultCount = userMapper.checkUserAccount(teacherAccount);
//        System.out.println(resultCount);
        if (resultCount == 0) {
            System.out.println("用户名不存在");
            return ServerResponse.createByErrorMessage("用户名不存在");
        } else {
            //MD5加密
            //使用MD5 jar包
            Mademd5 md5Password = new Mademd5();
            User user = userMapper.selectLogin(teacherAccount, md5Password.toMd5(teacherPassword));
            if (user == null) {
                return ServerResponse.createByErrorMessage("密码错误");
            }
            System.out.println("登陆成功");
//            System.out.println(user.getTeacherName());
//            Const.TEACHERNAME = user.getTeacherName();
            return ServerResponse.createBySuccess("登录成功", user);
        }
    }

    @Override
    public ServerResponse<User> register(String teacherName, String teacherAccount, String teacherPassword) {
        int Count = userMapper.checkUserAccount(teacherAccount);
        if (Count != 0) {
            return ServerResponse.createByErrorMessage("账号已存在");
        }
        if (teacherPassword == null || teacherPassword == "") {
            return ServerResponse.createByErrorMessage("注册失败");
        }
        User user = new User();
        user.setTeacherName(teacherName);
        user.setTeacherAccount(teacherAccount);
//        System.out.println(teacherName+teacherAccount+teacherPassword);
        //MD5加密
        //使用MD5 jar包
        Mademd5 md5Password = new Mademd5();
        user.setTeacherPassword(md5Password.toMd5(teacherPassword));

//        user.setUserLevel(LevelCode.ChoseLevel.ROLE_CUSTOMER);
        int resultCount = userMapper.insert(user);
        if (resultCount == 0) {
            return ServerResponse.createByErrorMessage("注册失败");
        }
        return ServerResponse.createBySuccessMessage("注册成功");
    }
}
